package com.last.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.last.entity.City;
import com.last.mapper.CityTaskMapper;
import com.last.service.ICityTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CityTaskServiceImpl extends ServiceImpl<CityTaskMapper, City> implements ICityTaskService {
    @Autowired(required = false)
    private CityTaskMapper cityMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void selectCityIncludeArea() {
        List<Map> cityListMap = cityMapper.selectCityIncludeArea();
        HashSet<Object> hashSet = new HashSet<>();

        for (Map map : cityListMap) {
            Set set = map.keySet();
            hashSet.add(map.get("city_name"));
        }
        for (Object o : hashSet) {
            ArrayList<String> strings = new ArrayList<>();
            HashMap<String, Object> objectHashMap = new HashMap<>();
            for (Map map : cityListMap) {
               if (map.get("city_name").toString().equals(o.toString())){
                   String value = map.get("area_name")+","+map.get("area_pic");
                   strings.add(value);
               }
            }
            String s = null;
            try {
                s = objectMapper.writeValueAsString(strings);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            stringRedisTemplate.boundHashOps("city").put(o.toString(),s);
        }
    }
}
